//
//  HJCComposeImageCollectionViewController.swift
//  新浪微博Swift
//
//  Created by 郝俊聪 on 15/5/22.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit
import SVProgressHUD


class HJCComposeImageCollectionViewController: UICollectionViewController {

    var pictures = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()


    }


  

    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return (pictures.count + 1)
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCompose", forIndexPath: indexPath) as! HJCComposeImageViewCell
        
        cell.delegate = self
        
        if indexPath.item == pictures.count{
           cell.image = nil
        }else{
           
           cell.image = pictures[indexPath.item]
        
        }
        
        return cell
    }

  

}

//MARK: - HJCComposeImageViewCellDelegate
extension HJCComposeImageCollectionViewController : HJCComposeImageViewCellDelegate{
    //点击选中图片
    func composeImageViewCellClickImageBtn(cell: HJCComposeImageViewCell) {
    
        //判断是否已经选取了照片
        if cell.image != nil{
           return
        }
        
        //先确认是否有权限访问相册
        if !UIImagePickerController.isSourceTypeAvailable( UIImagePickerControllerSourceType.PhotoLibrary){
            SVProgressHUD.showErrorWithStatus("没有权限访问相册")
        
        }
        
        //实例化照片选择器
        let picker = UIImagePickerController()
        
        
        //设置代理。通过代理获得照片
        picker.delegate = self
        
        //指定照片类型
        picker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        
        //. 指定是否允许编辑 - 如果让用户选择头像时，可以使用
        // 对于用户上传照片不合适
        //        picker.allowsEditing = true
        
        //跳转到照片选择控制器
        self.presentViewController(picker, animated: true, completion: nil)
        
    }
    //点击关闭图片
    func composeImageViewCellClickCloseBtn(cell: HJCComposeImageViewCell) {
            let indexPath = collectionView?.indexPathForCell(cell)
       let index = indexPath?.row
        if index < pictures.count{
           pictures.removeAtIndex(index!)
           //刷新
            self.collectionView?.reloadData()
        }

    }

}

//MARK：- UIImagePickerControllerDelegate
extension HJCComposeImageCollectionViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    //选择了照片
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        //将照片添加到数组中
//        var array = pictures as NSArray
//        println(image)
        //这里不能判断，因为每次同一张图片都不是同一个对象
//        if !array.containsObject(image)
//        {
        
          let scaleImage = image.imageOfScaleWithWidth(UIScreen.mainScreen().bounds.size.width)
        
            pictures.append(scaleImage)
//             println(scaleImage)
            //刷新数据
            self.collectionView?.reloadData()
//        }
        
        //关闭控制器
        dismissViewControllerAnimated(true, completion: nil)
    }
    

}
